package com.lenovo.codestatreport.git.service;

import com.lenovo.codestatreport.git.mapper.CodeStatOnlyMasterMapper;
import com.lenovo.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Howie Lu
 * @version Updated at 2019/06/03
 * @description
 */
@Service("CodeStatOnlyMasterService")
public class CodeStatOnlyMasterServiceImpl implements CodeStatOnlyMasterService {
    @Autowired
    CodeStatOnlyMasterMapper codeStatOnlyMasterMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private Utils utils = new Utils();

    @Override
    public List<Map<String, Object>> getStatsByDepartmentByTime(String start, String end, String department) {
        //用来做readNameFromFile方法的参数
        String tablelist = "";
        switch (department) {
            case "web":
                tablelist = "web-table";
                break;
            case "server":
                tablelist = "server-table";
                break;
        }
        //从配置文件读取人员名单
        String[] persons = utils.readNameFromFile(department);
        //从配置文件读取仓库列表
        String[] tables = utils.readNameFromFile(tablelist);
        RedisSerializer redisSerializer = new StringRedisSerializer();
        String key = "getStatsByDepartment" + department + "/From" + start + "to" + end;
        redisTemplate.setKeySerializer(redisSerializer);
        List<Map<String, Object>> list = (List<Map<String, Object>>) redisTemplate.opsForValue().get(key);
        if (list == null) {
            //无缓存，查找数据库
            list = new ArrayList<>();
            for (String table : tables) {
                for (String person : persons) {
                    //采用三元表达式" ? : "，处理空值情形
                    int totalAddLine = codeStatOnlyMasterMapper.countAddLineByPersonByTime(table, person, start, end) != null
                            ? codeStatOnlyMasterMapper.countAddLineByPersonByTime(table, person, start, end)
                            : 0;
                    int totalDelLine = codeStatOnlyMasterMapper.countDeleteLineByPersonByTime(table, person, start, end) != null
                            ? codeStatOnlyMasterMapper.countDeleteLineByPersonByTime(table, person, start, end)
                            : 0;
                    int totalLine = codeStatOnlyMasterMapper.countTotalLineByPersonByTime(table, person, start, end) != null
                            ? codeStatOnlyMasterMapper.countTotalLineByPersonByTime(table, person, start, end)
                            : 0;
                    int totalCommits = codeStatOnlyMasterMapper.countTotalCommitsByPersonByTime(table, person, start, end) != null
                            ? codeStatOnlyMasterMapper.countTotalCommitsByPersonByTime(table, person, start, end)
                            : 0;
                    //过滤没有参与某个仓库的人员
                    //只要代码提交次数为0，则说明该员工没有参与该仓库的编码工作
                    if (totalCommits == 0) {
                        break;
                    }
                    //System.out.println(person + " " + totalAddLine + " " + totalDelLine + " " + totalLine + " " + totalCommits);
                    Map<String, Object> map = new HashMap<>();
                    map.put("rd_name", person);
                    map.put("function_name", table);
                    map.put("total_add_line", totalAddLine);
                    map.put("total_del_line", totalDelLine);
                    map.put("total_line", totalLine);
                    map.put("total_commits", totalCommits);
                    map.put("branch", "master");
                    list.add(map);
                }
            }
        }
        return list;
    }
}
